﻿namespace LS.Nc6.Web.Entry.Areas.Systems.Controllers
{
    /// <summary>
    /// 登录日志
    /// </summary>
    [Route("[area]/[controller]/[action]")]
    [Area("Systems")]
    [ApiDescriptionSettings("Default")]
    public class LogsController : BaseAdminController
    {
        #region LogsController
       
        private readonly ISysLoginLogService _loginlogService;
        private readonly ISysOperateLogService _operatelogService;
        private readonly ICache _cache;
        public LogsController(ISysLoginLogService loginlogService, ISysOperateLogService operatelogService,  IOptions<CacheOptions> cacheOptions, Func<string, ISingleton, object> resolveNamed)
            : base(cacheOptions, resolveNamed)
        {
            _loginlogService = loginlogService;
            _operatelogService = operatelogService;
            _cache = resolveNamed(cacheOptions.Value.CacheType.ToString(), default) as ICache;
        }
        #endregion

        #region 页面
        public ActionResult Index()
        {
            SysPower(SysMenuConstant.MENU_LOGS_MANAGE, SysMenuConstant.FUN_SELECT, UserInfo.ID.ToString());
            return View();
        }
        #endregion

        #region  公共方法
        [HttpGet]
        public async Task<ResponseResultDataList> GetAllLogs()
        {
            string userName = RequestHelper.FilterParam(Request.Query["userName"]);
            string createtime = RequestHelper.FilterParam(Request.Query["createtime"]);

            var page = Request.Query["page"].ToString().GetQueryValueOrNull<int>(1);
            var limit = Request.Query["limit"].ToString().GetQueryValueOrNull<int>(20);
            string field = Request.Query["field"];
            string order = Request.Query["order"];
            var expable = Expressionable.Create<SysLoginLogEntity>();
            expable.AndIF(!string.IsNullOrEmpty(userName), a => a.LoginName.Contains(userName));

           
            if (!string.IsNullOrEmpty(createtime))
            {
                var ls=RequestHelper.GetDateForStr(createtime);
                if (ls.Count >= 2)
                {
                    expable.And(a => a.CreateDate> ls[0].ParseToDateTime() && a.CreateDate< (ls[1] + " 23:59:59").ParseToDateTime());
                }
            }
            var exp = expable.ToExpression();

            var strField = RequestHelper.FilterParam(field);
            var strOrder = RequestHelper.FilterParam(order);

            Expression<Func<SysLoginLogEntity, object>> expressionOrder;
            if (string.IsNullOrEmpty(strField) || string.IsNullOrEmpty(strOrder))
            {
                expressionOrder = (a) => new { a.Id };
            }
            else
            {
                //实现字段排序
                expressionOrder = (a) => new { a.CreateDate };
            }
            var result = await _loginlogService.PageList(exp, page, limit, expressionOrder);
            return new ResponseResultDataList { code = ResponseHelper.SEUCCESSCODE, message = "请求成功", data = result.Items, count = result.TotalCount };
        }
        #endregion

    }
}